package com.briup.store.service.impl;

import com.briup.common.exception.SystemException;
import com.briup.common.response.CodeAndMessageEnum;
import com.briup.common.utils.UserContext;
import com.briup.store.mapper.CollectMapper;
import com.briup.store.service.CollectService;
import com.briup.store.domain.vo.CollectGetListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【es_collect(收藏表)】的数据库操作Service实现
* @createDate 2024-12-24 09:16:52
*/
@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    private CollectMapper collectMapper;



    @Override
    public Boolean isCollect(Integer goodsId) {
        /**
         * 参数校验
         */

        /**
         * 根据用户ID以及商品ID查询收藏表
         *  - 查到数据：返回 true
         *  - 没查到数据： 返回false
         */
        Integer count = collectMapper.selectCountByUserAndGoods(UserContext.getUser(),goodsId);

        if (count >= 1){
            return true;
        }
        return false;
    }

    @Override
    public void remove(Integer goodsId) {
        collectMapper.delete(UserContext.getUser(),goodsId);
    }

    @Override
    public void add(Integer goodsId) {
        collectMapper.insert(UserContext.getUser(),goodsId);
    }

    @Override
    public List<CollectGetListVO> getCollectList() {
        return collectMapper.selectList(UserContext.getUser());
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        /**
         * 参数校验
         */
        if (Objects.isNull(ids) || ids.isEmpty()){
            throw new SystemException(CodeAndMessageEnum.COLLECT_IDS_IS_NULL);
        }

        /**
         * 批量删除
         */
        collectMapper.batchDelete(ids);
    }
}




